/*

This file is part of Rooms3D.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
Rooms3D is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
Rooms3D is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with Rooms3D; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/

package com.datasentinel.rooms.swing.util;

import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Rick Power
 * 
 */
public class TimedMouseEventHandler implements Runnable
{
	private static Logger logger = Logger.getLogger(TimedMouseEventHandler.class.getName());
	private MouseEvent event;
	private TimedMouseEventHandlerCallback callback;
	private long period = -1L;
	private long lastUpdateOn = -1L;
	private Thread runThread;

	public TimedMouseEventHandler(TimedMouseEventHandlerCallback callback, long period)
	{
		super();
		this.callback = callback;
		this.period = period;
	}
	
	public boolean isActive()
	{
		synchronized(this)
		{
			return runThread != null && runThread.isAlive();
		}
	}

	public void update(MouseEvent event)
	{
		synchronized(this)
		{
			if (runThread == null || !runThread.isAlive())
			{
				logger.fine(Thread.currentThread() + " creating new thread for callback:" + callback);
				runThread = new Thread(this, String.format("TimedMouseEventHandler<%s>", callback));
				runThread.start();
			}
		}
		this.event = event;
		lastUpdateOn = System.currentTimeMillis();
	}

	public void run()
	{
		logger.fine(Thread.currentThread() + " started");
		while (true)
		{
			try
			{
				if ((System.currentTimeMillis() - lastUpdateOn) > 250L)
				{
					logger.fine(Thread.currentThread() + "timing out");
					break;
				}
				if (event instanceof MouseEvent)
				{
					try
					{
						EventQueue.invokeAndWait(new Runnable() {
							public void run()
							{
								callback.handle(event);
								event = null;
							}
						});
					}
					catch (IllegalStateException e)
					{
						logger.log(Level.INFO, "error while handling mouse event", e);
					}
					catch (Exception e)
					{
						logger.log(Level.SEVERE, "error while handling mouse event", e);
						break;
					}
				}
				Thread.sleep(period == -1?50L:period);
			}
			catch (InterruptedException ie)
			{
				break;
			}
		}
		logger.fine(Thread.currentThread() + " finished");
	}
}
